
import { _decorator, Component, Label, Node, Toggle, Animation, AnimationClip, AnimationState, EventTouch } from 'cc';
import { GameData, PhaseTransitionType } from '../system/GameData';
import { DataHelper } from '../system/DataHelper';
import EvtManger from '../tools/EvtManger';
import { AppControll, ConfigKey, EventName, PopView } from '../manager/AppControll';
import { SoundManager } from '../manager/SoundManger';
import { PopManager } from '../manager/PopManager';
const { ccclass, property } = _decorator;


@ccclass('StageControl')
export class StageControl extends Component {
    @property(Label)
    mLbCurStage: Label = null;
    @property(Label)
    mLbAverageEarningsGold: Label = null;
    @property(Label)
    mLbAverageEarningsHonor: Label = null;
    @property(Toggle)
    mTgPhaseTransition: Toggle = null;
    @property(Node)
    mNdGuide: Node = null;

    private mIsHide: boolean = true;
    mBIsGuideLock: boolean = true;

    protected onLoad(): void {
        this.node.getComponent(Animation).on(Animation.EventType.FINISHED, () => {
            this.mIsHide = !this.mIsHide;
        }, this)
        EvtManger.on(EventName.monsterDeath, this, this.InitStageView);
        this.InitStageView();
        this.mNdGuide.getComponent(Animation).on(Animation.EventType.FINISHED, () => {
            this.mBIsGuideLock = false;
        })
    }

    OnBtnHide() {
        AppControll.ShelvedTime = 0;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        let state = this.node.getComponent(Animation).getState("contract");
        if (this.mIsHide) {
            state.speed = 1;
            state.wrapMode = AnimationClip.WrapMode.Normal;
            this.node.getComponent(Animation).play();
        } else {
            state.wrapMode = AnimationClip.WrapMode.Reverse;
            this.node.getComponent(Animation).play();
        }
    }

    protected onDestroy(): void {
        EvtManger.off(EventName.monsterDeath, this, this.InitStageView);
    }

    OnCloseGuide() {
        if (this.mBIsGuideLock) return;
        this.mNdGuide.getComponent(Animation).stop();
        this.mNdGuide.active = false;
    }

    InitStageView() {
        this.mLbCurStage.string = (GameData.playerData.CurStage - 10000) + "";
        let refreshConfig = DataHelper.GetMonsterRefreshConfig();
        this.mLbAverageEarningsGold.string = refreshConfig.stageAverageDropGold + "";
        this.mLbAverageEarningsHonor.string = refreshConfig.stageAverageDropHonor + "";
        this.mTgPhaseTransition.isChecked = GameData.playerData.PhaseTransition == PhaseTransitionType.automatic ? true : false;
        if (refreshConfig.stageID == Number.parseInt(DataHelper.GetConfigByKey(ConfigKey.stageGuild).value) && GameData.playerData.stageGuide) {
            GameData.playerData.stageGuide = false;
            this.mNdGuide.active = true;
            this.mNdGuide.getComponent(Animation).play();
        }
    }

    OnBtnStageAdd() {
        AppControll.ShelvedTime = 0;
        GameData.playerData.stageGuide = false;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        if (DataHelper.GetMonsterRefreshConfigById(GameData.playerData.CurStage + 1)) {
            GameData.playerData.CurStage += 1;
            GameData.playerData.PhaseTransition = PhaseTransitionType.lock;
            this.mTgPhaseTransition.isChecked = false;
            EvtManger.do(EventName.stageChange);
            this.InitStageView();
            GameData.SavePlayData();
        }
    }

    OnBtnReduce() {
        AppControll.ShelvedTime = 0;
        GameData.playerData.stageGuide = false;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        if (GameData.playerData.CurStage > 10001) {
            GameData.playerData.CurStage--;
        }
        GameData.playerData.PhaseTransition = PhaseTransitionType.lock;
        this.mTgPhaseTransition.isChecked = false;
        EvtManger.do(EventName.stageChange);
        this.InitStageView();
        GameData.SavePlayData();
    }

    OnToggleClick(event: EventTouch) {
        AppControll.ShelvedTime = 0;
        SoundManager.Instance<SoundManager>().PlayEffect(GameData.btnClick);
        if (!event.currentTarget.getComponent(Toggle).isChecked) {
            GameData.playerData.PhaseTransition = PhaseTransitionType.automatic;
        } else {
            GameData.playerData.PhaseTransition = PhaseTransitionType.lock;
        }
        EvtManger.do(EventName.stageChange);
        GameData.SavePlayData();
    }
}
